clear
 
local goodlist "breadcereals	meat	fish	milkcheeseeggs	oilfats	fruit	vegetables	sugar	foodproducts	coffeetea	waterjuice	spirits	wine	beer	TOBACCO	garments	otherclothing	clothingrepair	SHOES	RENT	maintmaterials	maintservices	water	sewerage	electricity	gas	liquidfuel	solidfuel	hgfurniture	carpets	TEXTILES	appliances	appliancerepair	GLASSTABLEWARE	TOOLS	hhnondurables	domservices	pharmaceutical	othermedical	medicalservices	dentalservices	HOSPITAL	newcar	shcar	allbikepurchase	spareparts	vehfuel	vehmaint	vehservices	railway	roadtrans	airtrans	seatrans	POSTAL	PHONE	avequip	photoequip	itequip	recordequip	equiprepair	RECDURABLES	games	sportequip	garden	pets	sportservices	cultureservices	books	newspapers	miscprinted	PKGHOLIDAYS	restrcafe	canteens	ACCOMSERVICES	hairdressing	pcproducts	jewellery	othereffects	SOCIALPROT	dwellinginsurance	medicalinsurance	transinsurance	FINSERVICES	OTHERSERVICES	EDUCATION"

local extendlist hhref	caseno	datayear	year	month `goodlist' totexp


forvalues n = 2001/2017 {
use "microdata\cpiexpenditure\cpicons`n'.dta",clear 

keep `extendlist'

merge m:1 hhref using "processed/demosave/demo`n'.dta",keep(match) nogen
	xtile quant = totexp [pweight=weight], nq(10) 
	xtile quint = totexp [pweight=weight], nq(5)
	pctile pp_value = totexp [pweight=weight], nq(10) genp(percent)

save processed/cpisave/cpi`n'.dta,replace
}


local goodlist "breadcereals	meat	fish	milkcheeseeggs	oilfats	fruit	vegetables	sugar	foodproducts	coffeetea	waterjuice	spirits	wine	beer	TOBACCO	garments	otherclothing	clothingrepair	SHOES	RENT	maintmaterials	maintservices	water	sewerage	electricity	gas	liquidfuel	solidfuel	hgfurniture	carpets	TEXTILES	appliances	appliancerepair	GLASSTABLEWARE	TOOLS	hhnondurables	domservices	pharmaceutical	othermedical	medicalservices	dentalservices	HOSPITAL	newcar	shcar	allbikepurchase	spareparts	vehfuel	vehmaint	vehservices	railway	roadtrans	airtrans	seatrans	POSTAL	PHONE	avequip	photoequip	itequip	recordequip	equiprepair	RECDURABLES	games	sportequip	garden	pets	sportservices	cultureservices	books	newspapers	miscprinted	PKGHOLIDAYS	restrcafe	canteens	ACCOMSERVICES	hairdressing	pcproducts	jewellery	othereffects	SOCIALPROT	dwellinginsurance	medicalinsurance	transinsurance	FINSERVICES	OTHERSERVICES	EDUCATION totexp"

local extendlist caseno	datayear `goodlist'
forvalues n = 2001/2017 {
	use microdata\cpiexpenditure\cpikidscons`n'.dta,clear
keep `extendlist'
	foreach x of varlist `goodlist' 	 {
		rename `x' kid`x' 
	}
save processed/cpisave/kidcpi`n'.dta,replace
}


local goodlist "breadcereals	meat	fish	milkcheeseeggs	oilfats	fruit	vegetables	sugar	foodproducts	coffeetea	waterjuice	spirits	wine	beer	TOBACCO	garments	otherclothing	clothingrepair	SHOES	RENT	maintmaterials	maintservices	water	sewerage	electricity	gas	liquidfuel	solidfuel	hgfurniture	carpets	TEXTILES	appliances	appliancerepair	GLASSTABLEWARE	TOOLS	hhnondurables	domservices	pharmaceutical	othermedical	medicalservices	dentalservices	HOSPITAL	newcar	shcar	allbikepurchase	spareparts	vehfuel	vehmaint	vehservices	railway	roadtrans	airtrans	seatrans	POSTAL	PHONE	avequip	photoequip	itequip	recordequip	equiprepair	RECDURABLES	games	sportequip	garden	pets	sportservices	cultureservices	books	newspapers	miscprinted	PKGHOLIDAYS	restrcafe	canteens	ACCOMSERVICES	hairdressing	pcproducts	jewellery	othereffects	SOCIALPROT	dwellinginsurance	medicalinsurance	transinsurance	FINSERVICES	OTHERSERVICES EDUCATION totexp"


forvalues n = 2001/2017 {
	use "processed/cpisave/cpi`n'.dta", clear
	merge m:1 datayear caseno using processed/cpisave/kidcpi`n'.dta
	local count = 1
	foreach x of varlist `goodlist' 	 {
replace `x' = `x' + kid`x' if kid`x' != .
drop kid`x'
		rename `x'  exp_`count'
	local count = `count' + 1
}
	drop _merge
	rename exp_86 I 
save "processed/cpisave/cpisum`n'.dta",replace
}


use "processed/cpisave/cpisum2001.dta", clear
forvalues n = 2002/2017 {
append using processed/cpisave/cpisum`n'.dta
}
forvalues n = 1/85 {
	gen b`n' = exp_`n'/I
}
	drop exp_*
save processed/cpisave/cpi_master.dta,replace

*** price 
use price/cpipdata.dta,clear
gen date_numeric = date(yrm, "Ym")

local goodlist "breadcereals	meat	fish	milkcheeseeggs	oilfats	fruit	vegetables	sugar	foodproducts	coffeetea	waterjuice	spirits	wine	beer	TOBACCO	garments	otherclothing	clothingrepair	SHOES	RENT	maintmaterials	maintservices	water	sewerage	electricity	gas	liquidfuel	solidfuel	hgfurniture	carpets	TEXTILES	appliances	appliancerepair	GLASSTABLEWARE	TOOLS	hhnondurables	domservices	pharmaceutical	othermedical	medicalservices	dentalservices	HOSPITAL	newcar	shcar	allbikepurchase	spareparts	vehfuel	vehmaint	vehservices	railway	roadtrans	airtrans	seatrans	POSTAL	PHONE	avequip	photoequip	itequip	recordequip	equiprepair	RECDURABLES	games	sportequip	garden	pets	sportservices	cultureservices	books	newspapers	miscprinted	PKGHOLIDAYS	restrcafe	canteens	ACCOMSERVICES	hairdressing	pcproducts	jewellery	othereffects	SOCIALPROT	dwellinginsurance	medicalinsurance	transinsurance	FINSERVICES	OTHERSERVICES EDUCATION"

gen datayear = year(date_numeric)
gen month = month(date_numeric)

keep datayear `goodlist' 
collapse (mean) `goodlist', by(datayear)


local count = 1
foreach x of varlist `goodlist' 	 {
	rename `x'  p`count'
	local count = `count' + 1
}
keep if datayear > 2000
keep if datayear < 2018
drop if datayear == .

*save 
outsheet using DataforMatlab\p_CPI.csv, comma replace nonames


tset datayear
forvalues n = 1/85 {
	gen dlogp`n' = log(p`n') - log(l.p`n')
}
drop p*

save processed/cpisave/dlogcpi_master.dta,replace

* Engel:
use processed/cpisave/cpi_master.dta,clear
drop if I ==.
gen logI = log(I)
qui forvalues x = 1/85	 {
	lowess b`x' logI  if datayear ==2001, generate(tmp) nograph
	gen b`x'_hat = tmp
	drop tmp
}

qui forvalues t = 2002/2017 {

qui forvalues x = 1/85 {
	lowess b`x' logI  if datayear == `t', generate(tmp) nograph
	replace b`x'_hat = tmp if datayear == `t'
	drop tmp
} 
}

keep datayear I *_hat weight caseno
duplicates drop caseno datayear,force
drop caseno

outsheet using DataforMatlab\UK_Budget_CPI.csv, comma replace nonames
